package com.ruoyi.statisticalCenter.controller;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import cn.hutool.core.net.URLEncoder;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.framework.cache.CustomCacheFilter;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.logistic.utils.TableUtils;
import com.ruoyi.statisticalCenter.domain.ScCashFlow;
import com.ruoyi.statisticalCenter.domain.vo.*;
import com.ruoyi.statisticalCenter.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statisticalCenter.domain.ScWeightFlowDirectionMonitoring;
import com.ruoyi.statisticalCenter.service.IScWeightFlowDirectionMonitoringService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 均重流向监控Controller
 *
 * @author pyx
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/sc/monitoring")
@Api(tags = "统计中心-均重流向监控")//流向均重监控表
public class ScWeightFlowDirectionMonitoringController extends BaseController {
    @Autowired
    private IScWeightFlowDirectionMonitoringService scWeightFlowDirectionMonitoringService;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private TableService tableService;
    @Autowired
    private RedisUtil redisUtil;

    //@CustomCacheFilter
    @PostMapping("/collect")
    @ApiOperation("汇总")
    public R listNew(@RequestBody ScWeightFlowDirectionMonitoringVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        return scWeightFlowDirectionMonitoringService.collectNew(vo);
    }

    @ApiOperation("cache")
    @PostMapping("cache")
    public R cache(@RequestBody DateStrVo time) {
        return scWeightFlowDirectionMonitoringService.cache(time);
    }


    /**
     * 查询均重流向监控列表
     */
//    @PostMapping("/list")
//    @ApiOperation("查询均重流向监控列表")
//    public R list(@RequestBody ScWeightFlowDirectionMonitoringVo vo) {
//        if (vo != null) {
//            String startTime = vo.getStartTime().replace("-", "");
//            String endTime = vo.getEndTime().replace("-", "");
//            vo.setStartTime(startTime);
//            vo.setEndTime(endTime);
//        }
//
//        String list = scWeightFlowDirectionMonitoringService.selectDisposition().toString();
//
//        // 构建Redis键
//        String redisKey = "sc_weight_flow_direction_monitoring:collect:" + vo.getStartTime() + ":" + vo.getEndTime() + list + vo.getPageDate().getPageNum() + vo.getPageDate().getPageSize();
//        // 检查Redis中是否存在数据
//        R redisResult = redisUtil.getObject(redisKey, R.class);
//        if (redisResult != null) {
//            return redisResult; // 如果Redis中存在数据，则直接返回
//        }
//        // 如果Redis中不存在数据，则从数据库中查询
//        R databaseResult = scWeightFlowDirectionMonitoringService.collect(vo);
//        // 将数据库查询结果存储到Redis，并设置过期时间为一天
//        redisUtil.setObject(redisKey, databaseResult, 600);
//        return databaseResult;
//    }
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(@RequestBody ScWeightFlowDirectionMonitoringVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
            PageDate pageDate = new PageDate();
            pageDate.setPageNum(1);
            pageDate.setPageSize(10000);
            vo.setPageDate(pageDate);
        }
        String fileName = "attachment;filename=" + URLEncoder.createDefault().encode("流向均重监控报表123", Charset.forName("utf-8")) + ".xls";
        response.setHeader("Content-Disposition", fileName);
//        List<ScWeightFlowDirectionMonitoring> list = scWeightFlowDirectionMonitoringService.collectList(scWeightFlowDirectionMonitoringVo);
//        ExcelUtil<ScWeightFlowDirectionMonitoring> util = new ExcelUtil(ScWeightFlowDirectionMonitoring.class);
//        util.exportExcel(response, list, "流向均重监控报表");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        scWeightFlowDirectionMonitoringService.collectList(vo, outputStream);
    }

    @PostMapping("/updateDisposition")
    @ApiOperation("修改均重流向配置参数")
    public R updateDisposition(@RequestBody ScWeightFlowDirectionDispositionVo scWeightFlowDirectionDispositionVo) {
        return scWeightFlowDirectionMonitoringService.updateDisposition(scWeightFlowDirectionDispositionVo);
    }

    @GetMapping("/selectDisposition")
    @ApiOperation("查询均重流向配置参数")
    public R selectDisposition() {
        return scWeightFlowDirectionMonitoringService.selectDisposition();
    }


    @ApiOperation("点击跳转")
    @PostMapping("/point")
    public R point(@RequestBody ScWeightFlowDirectionDispositionPointVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        List<HqNetworkBillDetail> list = scWeightFlowDirectionMonitoringService.point(vo);
        List<TableVo> fieldList = tableService.getFieldAnnotation(new HqNetworkBillDetail());
        return TableUtils.handleListResponse(list, fieldList, "暂无数据");
    }
}
